package com.vincent.filepicker.adapter;

import com.vincent.filepicker.ResourceTable;
import com.vincent.filepicker.filter.entity.Directory;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * Created by Vincent Woo
 * Date: 2018/2/27
 * Time: 10:25
 */

public class FolderListAdapter extends BaseAdapter<Directory> {
    private FolderListListener mListener;

    public FolderListAdapter(Context ctx, ArrayList<Directory> list) {
        super(ctx, list);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        FolderListViewHolder viewHolder = null;
        if (component == null) {
            viewHolder = new FolderListViewHolder();
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_vw_layout_item_folder_list, null, false);
            viewHolder.mTvTitle = (Text) component.findComponentById(ResourceTable.Id_tv_folder_title);
            component.setTag(viewHolder);
        } else {
            viewHolder = (FolderListViewHolder) component.getTag();
        }

        viewHolder.mTvTitle.setText(mList.get(position).getName());
        component.setClickedListener(component1 -> {
            if (mListener != null) {
                mListener.onFolderListClick(mList.get(position));
            }
        });

        return component;
    }

    class FolderListViewHolder {
        private Text mTvTitle;
    }

    public interface FolderListListener {
        void onFolderListClick(Directory directory);
    }

    public void setListener(FolderListListener listener) {
        this.mListener = listener;
    }
}
